#!/usr/bin/env perl

#
# Copyright (c) 2002-2004 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration
# Environment (CABIE)
#
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying
# permission or http://www.gnu.org.
#
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK.
#
# Permission to modify the code and to distribute modified code is granted,
# provided the above notices are retained, and a notice that the code was
# modified is included with the above copyright notice.
#

BEGIN {
    push @INC, "lib";
    push @INC, "../lib";
    push @INC, "../../lib";
}

use Sys::Hostname;

#
# Get name of the build server
#
my $hostname = hostname();
$hostname    =~ s/\.[a-zA-Z0-9\n]+//g;
$hostname    =~ s/-//g;

my $ospackage;

if ($ =~ /MSWin32/) {
    $ospackage = "winsys";
} else {
    $ospackage = "unixsys";
}

#
# Grab configuration data from buildconf
#
require "$ospackage.pm";
require "$hostname.pm";

my $os     = new $ospackage;
my $config = new $hostname;

my $cmd = shift;

&$cmd(@ARGV);

sub hello {

    my $message = shift;

    print "$message\n";

}

sub changestate {

    my $job     = shift;
    my $message = shift;

    my $sqlquery;
    my @sqlarray;

    $sqlquery = "select message from joblog where binary server=\"$hostname\" ".
                "and binary title=\"$job\" and step=\"3\"";

    @sqlarray  = $os->run_sql_query("$sqlquery", ";", 0);

    my ($jobno) = split(/ /,$sqlarray[0]);

    $sqlquery = "update joblog set message=\"$message\" where ".
                "binary server=\"$hostname\" and binary title=\"$job\" ".
                "and step=\"3\"";

    @sqlarray  = $os->run_sql_query("$sqlquery", ";", 0);

    print "$message\n";

}
